//
//  BanJiXueShengView.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/4/15.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "BanJiXueShengView.h"
#import "SYRequestAPI+Teach.h"


@implementation BanJiXueShengView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/   
- (instancetype)initWithFrame:(CGRect)frame{
    if ([super initWithFrame:frame]) {
        [self initSubView];
    }
    return self;
}
- (void)initSubView{
    pageSize = 10;
    pageIndex = 1;
    _dataArray = [NSMutableArray new];
    self.backgroundColor = CONTENTBACKCOLOR;
    [self addSubview:self.tableList];
    [_tableList mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.bottom.equalTo(@0);
        make.top.equalTo(@0);
    }];
    [_tableList headerRefreshBlock:^{
//        [self->_tableList.mj_header beginRefreshing];
        self->pageIndex = 1;
        [self->_dataArray removeAllObjects];
        [self getBanJiXueShengRequest];
    }];
    [_tableList fooderRefreshBlock:^{
//        [self->_tableList.mj_footer beginRefreshing];
        self->pageIndex = ++self->pageIndex;
        [self getBanJiXueShengRequest];
    }];
}
- (void)reloadIndex{
    pageIndex = 1;
    [_dataArray removeAllObjects];
    [self getBanJiXueShengRequest];
}
- (UITableView *)tableList{
    if (!_tableList) {
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, APP_HEIGHT-84) style:UITableViewStylePlain];
        tableView.delegate  =self;
        tableView.dataSource = self;
        tableView.backgroundColor = [UIColor whiteColor];
        [tableView registerClass:[BanJiXueShengCell class] forCellReuseIdentifier:@"BanJiXueShengCell"];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _headerView = [[BanJiViewListTopHeader alloc]init];
        _headerView.frame = CGRectMake(0, 0,self.mainw, 131);
        tableView.tableHeaderView = _headerView;
        _tableList = tableView;
    }
    return _tableList;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BanJiXueShengCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BanJiXueShengCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[BanJiXueShengCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BanJiXueShengCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row%2 == 0) {
        cell.backgroundColor = [UIColor whiteColor];
    }else{
        cell.backgroundColor = [UIColorChange colorwithHexString:@"#F8F8F8"];
    }
    cell.index = (int)indexPath.row;
    [cell reloadViewLayout];
    UILabel *labelXueHaoLabel = (UILabel *)[cell viewWithTag:20];//学号
    UILabel *labelNameLabel = (UILabel *)[cell viewWithTag:21];//姓名
    UILabel *labelGenderLabel = (UILabel *)[cell viewWithTag:22];//性别
    UILabel *labelLianXiRenLabel = (UILabel *)[cell viewWithTag:23];//联系人
    UILabel *labelScoreLabel = (UILabel *)[cell viewWithTag:24];//电话
    UILabel *labelStatusLabel = (UILabel *)[cell viewWithTag:25];//状态
    UILabel *labelInTimeLabel = (UILabel *)[cell viewWithTag:26];//状态
    BanJiStudentListModel *model = _dataArray[indexPath.row];
    
    labelXueHaoLabel.text = model.serial;
    labelNameLabel.text = model.name;
    if ([model.sex isEqualToString:@"1"]) {
        labelGenderLabel.text = @"男";
    }else if ([model.sex isEqualToString:@"2"]){
        labelGenderLabel.text = @"女";
    }else{
        labelGenderLabel.text = @"未知";
    }
    labelLianXiRenLabel.text = model.father;
    labelScoreLabel.text = model.mobile;
    labelStatusLabel.text = model.statusName;
    labelInTimeLabel.text = model.inDate;
    
    cell.block = ^(int index) {
        //前往学生详情
        BanJiStudentListModel *model = self->_dataArray[index];
        [self.mainDelegate addBanJiXueShengView:model];
    };
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"点击了cell");
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (!_titleView) {
        _titleView = [[SJXQHeaderTitleView alloc]initWithFrame:CGRectMake(0, 1, self.frame.size.width, 44) andArray:@[@"学号",@"姓名",@"性别",@"联系人",@"电话",@"状态",@"入学日期",@"详情"]];
        _titleView.backgroundColor = CONTENTBACKCOLOR;
    }
    _titleView.frame = CGRectMake(0, 0, self.frame.size.width, 44);
    return _titleView;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *mainView = [[UIView alloc]init];
    return mainView;
}
//顶部结业日期显示状态
- (void)updateModel{
    [_headerView setTtitleLabelText:_mainModel.className];
    [_headerView setDangQianJinDuLabelText:_mainModel.schedule];
    [_headerView setKeChengLabelText:self.courseName];
    [_headerView setRenKeLaoShiLabelText:self.renKeLaoShiName];
    [_headerView setNianJiLabelText:_mainModel.typeName];
    [_headerView setShangKeShiJianLabelText:_mainModel.openDate];
    [_headerView setKeMuLabelText:_mainModel.statusName];
    [_headerView setZhaoShengRenShuLabelext:[NSString stringWithFormat:@"%@/%@",self.students,_mainModel.maxStudents]];
    [_headerView setJiaoShiLabelText:[NSString stringWithFormat:@"%@-%@",_mainModel.campusName,_mainModel.classroom]];
    _headerView.jieYeShiJianLabel.hidden = YES;
//    [_headerView setJieYeShiJianLabelText:_mainModel.finishedDate];
}


#pragma mark - 教学-班级-未上课时/已上课时 -班级学生
- (void)getBanJiXueShengRequest{
    NSDictionary *parama = @{@"classId":_classID,
                             @"pageIndex":[NSString stringWithFormat:@"%d",pageIndex],
                             @"pageSize":[NSString stringWithFormat:@"%d",pageSize]};
    [MBProgressHUD mj_hideHUDForView:self.tableList];
    
    //[MBProgressHUD mj_showMessage:nil toView:self.tableList];
    [SYRequestAPI sy_getClassServiceClassStudentList:parama success:^(id  _Nonnull responseObject) {
        NSArray *array = [responseObject objectForKey:@"data"];
        if (self->pageIndex == 1) {
            [self->_dataArray removeAllObjects];
        }
        
        for (NSDictionary *dic in array) {
            BanJiStudentListModel *model = [BanJiStudentListModel  yy_modelWithDictionary:dic];
            model.name = [dic nilObjectForKey:@"name"];
            model.statusName = [dic nilObjectForKey:@"statusName"];
            [self->_dataArray addObject:model];
        }
        [self.tableList reloadData];
        NSLog(@"教学-班级-未上课时/已上课时-得到班级学生列表 %@",responseObject);
        [MBProgressHUD mj_hideHUDForView:self.tableList];
        [self->_tableList.mj_header endRefreshing];
        [self->_tableList.mj_footer endRefreshing];
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUDForView:self.tableList];
        [self->_tableList.mj_header endRefreshing];
        [self->_tableList.mj_footer endRefreshing];
    }];
    
}
@end
